package com.teapos.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teapos.service.CoreService;
import com.teapos.util.ThreadLocalUtil;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/core")
@Controller
public class CoreController extends BaseController {
	
	@Autowired
	private CoreService coreService;

	@ResponseBody
	@RequestMapping("/pay")
	public String pay(HttpServletRequest req) {
		ThreadLocalUtil.put("user", super.getUser(req));
		try {
			String orderId = "0";
			orderId = coreService.checkAndSaveOrder(req);
			Map map = new HashMap();
			map.put("orderId", orderId);
			return success("下单成功！！",map);
		} catch (Exception e) {
			e.printStackTrace();
			return fail(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/cancel")
	public String cancel(String orderId) {
		try {
			coreService.cancelOrder(orderId);
			return success("撤销成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("撤销失败！！");
		}
	}
	
}
